package edu.caltech.cs.techerlink;

import java.io.IOException;
import javax.servlet.http.*;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import java.util.List;
import javax.jdo.PersistenceManager;

import edu.caltech.cs.techerlink.PMF;
import edu.caltech.cs.techerlink.Post;

@SuppressWarnings("serial")
public class mobileTecherlinkServlet extends HttpServlet {
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();

		PersistenceManager pm = PMF.get().getPersistenceManager();
		String query = "select from " + Post.class.getName()
				+ " order by date desc range 0,10";
		List<Post> posts = (List<Post>) pm.newQuery(query).execute();
		if (posts.isEmpty()) {
			resp.getWriter().println(
					"No messages yet. Be the first one to post!\n");
		} else {
			for (Post g : posts) {
				if (g.getAuthor() == null) {
					resp.getWriter().println("An anonymous person wrote: ");
				} else {
					resp.getWriter().println(
							g.getAuthor().getNickname() + "wrote: ");
				}
				resp.getWriter().println(g.getContent() + "\n");
			}
		}
		pm.close();
	}
}
